package org.pcorp.battlespace.construction.modele;

import java.io.FileReader;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

import org.pcorp.battlespace.modele.Equipement;

@XmlRootElement(name="equipements")
@XmlAccessorType(XmlAccessType.FIELD)
public class Equipements {
	@XmlElementWrapper(name = "equipements")
	@XmlElement(name = "equip")
	private List<Equipement> equipements;

	public List<Equipement> getEquipements() {
		return equipements;
	}

	public void setEquipements(List<Equipement> equipements) {
		this.equipements = equipements;
	}

	public static List<Equipement> getEquipement(String xml) {
		//String EQUIP_XML = "./equips.xml";
		Equipements equips = null;
		try {
		JAXBContext context = JAXBContext.newInstance(Equipements.class);
		Unmarshaller um = context.createUnmarshaller();
		
		equips = (Equipements) um.unmarshal(new FileReader(xml));
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return equips.getEquipements();
	}
	
	public static void main(String[] args) throws Exception {
		String EQUIP_XML = "./equips.xml";
		JAXBContext context = JAXBContext.newInstance(Equipements.class);
		Unmarshaller um = context.createUnmarshaller();
		Equipements equips = (Equipements) um.unmarshal(new FileReader(EQUIP_XML));

		for (int i = 0; i < equips.getEquipements().size(); i++) {
			System.out.println("equip " + (i + 1) + ": " + equips.getEquipements().get(i).getNom());
		}

		try {
			JAXBContext jaxbContext = JAXBContext.newInstance(Equipements.class);
			Marshaller marshaller = jaxbContext.createMarshaller();
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));
			marshaller.marshal(equips, System.out);
		} catch (Exception e) {e.printStackTrace();}
	}
}
